package cn.zyjblogs.server.client.controller;

import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@Api(tags = {"客户端管理"})
@RestController
@ApiVersion(1)
@RequestMapping("/{v}/client")
@ResponseBody
@RequiredArgsConstructor
public class OauthClientController {
    private final ClientDetailsService oauthClientDetailsService;

    @GetMapping("/findScopeList")
    @ApiVersion(1)
    public ResponseObject<Set<String>> getList(String clientId) {
        ClientDetails clientDetails = oauthClientDetailsService.loadClientByClientId(clientId);
        if (clientDetails == null) {
            return ResponseResult.error(HttpCode.BAD_REQUEST, "该客户端不存在");
        }
        return ResponseResult.success(clientDetails.getScope());
    }
}
